<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ page import="com.onlineMIS.ORM.entity.chainS.inventoryFlow.ChainInventoryFlowOrder,com.onlineMIS.ORM.entity.chainS.user.ChainRoleType" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>千禧宝贝连锁店管理信息系统</title>
<%@ include file="../../common/Style.jsp"%>
<script>
$(document).ready(function(){
	parent.$.messager.progress('close'); 
	$("#org_table tr").mouseover(function(){      
		$(this).addClass("over");}).mouseout(function(){    
		$(this).removeClass("over");}); 																																	
});
var baseurl = "<%=request.getContextPath()%>";


function downloadOrderTwo(){

    document.chainInventoryFlowForm.action="headqInventoryFlowJSPAction!downloadFlowOrder";
    document.chainInventoryFlowForm.submit();
}


//variable to identify it is form submit or close the page
var formSubmit = false;
var P_NUMBER = 2;
var N_PRINT = 1;
var P_DISCOUNT = 3;
var index = 0;

//constants
var SALES_ORDER = 1;
var RETURN_ORDER = 2;

var errorIndexes = new Array();

document.onkeydown = BSkeyDown; 
function BSkeyDown(e){

	 var ieKey = event.keyCode;
	 //enter key is pressed 
	 if (ieKey==13){
	   if (event.srcElement.id.substring(0,7)=="barcode"){
           var index_trigger = event.srcElement.id.substring(7); 

		   var currentBarcode =$("#barcode"+index_trigger).val();
	       event.returnValue=false;
	
	       retrieveProductByBarcode(index_trigger,"" , currentBarcode);
	   } else if (event.srcElement.id.substring(0,11)=="productCode"){
		   var index_trigger = event.srcElement.id.substring(11);
		   event.returnValue=false;
		   searchProductsProductCodeTF(index_trigger);
	   }else
		   event.returnValue=false;
	    
	 //back space key
	 }  else if (ieKey==8){
		    if((event.srcElement.tagName.toLowerCase() == "input" && event.srcElement.readOnly != true) || event.srcElement.tagName.toLowerCase() == "textarea"){
		        event.returnValue = true; 
		    } else
		    	event.returnValue = false;
	 }
} 


function changeFromChain(newValue){
	//var fromChainStoreId = $("#chainStore").combobox('getValue');
	var params = "formBean.flowOrder.fromChainStore.chain_id=" + newValue;
	//$.post(baseurl + "/actionChain/inventoryFlowJSONAction!changeFromChainStoreTransfer",params, backProcessChangeFromChainStore,"json");
	$('#toChainStore').combobox('clear');
	$('#toChainStore').combobox('reload',baseurl + "/actionChain/inventoryFlowJSONAction!changeFromChainStoreTransfer?"+params);
}

/**
 * retrieve the product by input barcode
 */
function retrieveProductByBarcode(index_trigger, suffix, currentBarcode){

	if (validateRowInput(index_trigger,currentBarcode)){		
	    var params= "formBean.barcode=" + currentBarcode + "&formBean.index=" + index_trigger+ "&formBean.chainId=" + $("#chainStore").combobox("getValue") ; 

	    $.post("actionChain/chainSalesJSONAction!scanByBarcode",params, backRetrievePByBarcodeProcess,"json");

	}
}

/**
 * user input the product code and press enter
 */
function searchProductsProductCode(index_trigger){
	var productCode = $.trim($("#productCode" +index_trigger).val());

	if (validateProductCodeInput(productCode)){
	    var url = encodeURI(encodeURI("actionChain/chainSalesJSPAction!scanByProductCode?" + "formBean.productCode=" + productCode + "&formBean.index=" + index_trigger +"&formBean.chainId=" + $("#chainStore").val())) ; 
		
	    window.open(url,'新窗口','height=530, width=600, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no');    
	}
}



/**
 * to validate the product code input
 */
function validateProductCodeInput(productCode){
	if (productCode.length <= 1){
       alert("输入的货号太短");
       return false;
	}
	return true;
}


/**
 * to clear the corresponding row's data
 */
function clearRowCells(index_c){
	//clear the cell
	$("#productId" +index_c).attr("value","");  
   	$("#unit" +index_c).attr("value","");  
	$("#brand" +index_c).attr("value","");   
	$("#productCode"+index_c).attr("value",0); 
	$("#quantity"+index_c).attr("value",0); 
	$("#comment"+index_c).attr("value",""); 
	$("#barcode"+index_c).attr("value",""); 
	$("#year"+index_c).attr("value",""); 
	$("#quarter"+index_c).attr("value",""); 
	$("#inventoryQ"+index_c).attr("value",""); 
	calculateTotal();
}




function deleteRow(rowID){
	$("#"+rowID).remove(); 
 	calculateTotal();
    
}

function deleteOrder(){
	if (confirm("你确定删除此单据吗?")){
		   formSubmit = true;
		   document.chainInventoryFlowForm.action = "actionChain/inventoryFlowJSPAction!deleteOrder";
		   document.chainInventoryFlowForm.submit();
	}
}

function validateTransferOrderForm(){
	
	var fromChainStoreId = $("#chainStore").combobox('getValue');
	//alert(fromChainStoreId)
	var toChainStoreId = $("#toChainStore").combobox('getValue');
	//alert(toChainStoreId)
	
	if (!isValidNonNegativeInteger(fromChainStoreId)){
		alert("请选择正确的 调出连锁店 ");
		return false;
	}
	//alert(isValidNonNegativeInteger(toChainStoreId));
	if (!isValidNonNegativeInteger(toChainStoreId)){
		alert("请选择正确的 调入连锁店 ");
		return false;
	}	
	
	if (fromChainStoreId == toChainStoreId){
		alert("调入连锁店 与 调出连锁店不能为同一个连锁店选项");
		return false;
	} else {
		var error = "";
		var hasChar_q = false;

		for (var i = 0; i < index; i++){

			var q = $("#quantity" + i).val();

			if (q != undefined){
				if (isNaN(q) || q<0){
					hasChar_q = true;
					errorIndexes.push(i);
				}
			}
		}
		
		if (hasChar_q)
			error += "数量 - 必须为大于0的整数. 请检查后重新输入!\n";

		if (error != ""){
			for (var i =0; i < errorIndexes.length; i++){
				$("#orderRow" + errorIndexes[i]).css('background-color', '#EE8553');
			}
			
			alert(error);
			return false;
		}
			var totalQ = $("#totalQuantity").val();
			var file = $("#inventory").val();
			if ((totalQ =="" || totalQ==0) && file == ""){
				alert("请输入货品或者导入盘点文件后再保存单据!");
				return false;
			}
            return true;

	}

}





function saveFinal(){
	if (validateTransferOrderForm()){
		   formSubmit = true;
		   document.chainInventoryFlowForm.action = "actionChain/inventoryFlowJSPAction!saveToFinal";
		   document.chainInventoryFlowForm.submit();
	}
}


/**
 * actions
 */
function saveDraft(){
	if (validateTransferOrderForm()){
	   formSubmit = true;
	   document.chainInventoryFlowForm.action = "actionChain/inventoryFlowJSPAction!saveToDraft";
	   document.chainInventoryFlowForm.submit();
	}
}

function cancelOrder(){
	if (confirm("你确定红冲此单据吗?")){
		   document.chainInventoryFlowForm.action = "actionChain/inventoryFlowJSPAction!cancelOrder";
		   document.chainInventoryFlowForm.submit();
	}
}

/**
 * user input the product code and press enter
 */
function searchProductsProductCodeTF(index_trigger){
	var productCode = $.trim($("#productCode" +index_trigger).val());
	var fromChainStoreId = $("#chainStore").combobox('getValue');
	var toChainStoreId = $("#toChainStore").combobox('getValue');
	var chainId = fromChainStoreId;
	if (chainId == 0)
		chainId = toChainStoreId;	
	
	if (validateProductCodeInput(productCode)){
	    var url = encodeURI(encodeURI("actionChain/chainSalesJSPAction!scanByProductCode?" + "formBean.productCode=" + productCode + "&formBean.index=" + index_trigger +"&formBean.chainId=" + chainId)) ; 
		
	    window.open(url,'新窗口','height=530, width=600, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no');    
	}
}

/**
 * 打印单子到小票机
 */
function printOrder(){
	
}

</script>
</head>
<body>
    <s:form action="/actionChain/inventoryFlowAction!editOrder" method="POST" name="chainInventoryFlowForm" id="chainInventoryFlowForm" theme="simple">
	<s:hidden name="formBean.flowOrder.id"/>   
    <table width="82%" align="center"  class="OuterTable">
	    <tr><td>
	         <div class="errorAndmes"><s:actionerror cssStyle="color:red"/><s:actionmessage cssStyle="color:blue"/></div>
			 <table width="100%" border="0">
			    <tr>
			       <td height="50" colspan="7">
				   	 <table width="100%" border="0">
				       <tr class="PBAOuterTableTitale">
				         <td height="32" colspan="5">
				              	<s:property value="uiBean.flowOrder.typeChainS"/>
				         </td>
			           </tr>
				       <tr class="InnerTableContent">
				         <td width="200" height="32">调出:<s:property value="uiBean.flowOrder.fromStore.name"/></td>
				         <td width="200">调入:<s:property value="uiBean.flowOrder.toStore.name"/></td>
				         <td width="120">创建人:<s:property value="uiBean.flowOrder.creator.name"/></td>
				         <td width="200">日期:<s:property value="uiBean.flowOrder.orderDate"/></td>
				         <td width="134">状态:
				         	 <s:if test="uiBean.flowOrder.status == 3">
				              		 <font style="color:red"><s:property value="uiBean.invenOrderStatus"/></font>
				              </s:if><s:else>
				                     <s:property value="uiBean.flowOrder.statusS"/>
				              </s:else>
				         </td>
				       </tr>
				     </table></td>
			    </tr>
			    <tr class="InnerTableContent">
			      <td colspan="7"><hr width="100%" color="#FFCC00"/></td>
			    </tr>
			    <tr>
			      <td colspan="7">
			            <!-- table to display the product information -->
						<table width="100%"  align="left" class="OuterTable" id="org_table">
							<tr class="PBAInnerTableTitale">
						    <th width="3%" height="35">&nbsp;</th>
						    <th width="5%">年份</th>
						    <th width="5%">季度</th>
						    <th width="12%">品牌</th>
						    <th width="10%">货号</th>
						    <th width="10%">颜色</th>
						    <th width="12%">条形码</th>
						    <th width="10%">单位</th>
						    <th width="8%">零售单价</th>
						    <th width="8%">数量</th>
						    <th width="8%">零售额</th>
						    <th width="15%">摘要</th>
						  </tr>
						  <tbody id="orderTablebody">
						      <s:iterator value="uiBean.flowOrder.productList" status = "st" id="orderProduct" >
						  		<tr class="InnerTableContent" align='center' id="orderRow0" class="InnerTableContent" <s:if test="#st.odd">style='background-color: rgb(255, 250, 208);'</s:if>>   
							      <td height="25"><s:property value="#st.index +1"/></td>							      
							      <td><s:property value="#orderProduct.productBarcode.product.year.year"/></td>
							      <td><s:property value="#orderProduct.productBarcode.product.quarter.quarter_Name"/></td>
							      <td><s:property value="#orderProduct.productBarcode.product.brand.brand_Name"/></td>
							      <td><s:property value="#orderProduct.productBarcode.product.productCode"/></td>
							      <td><s:property value="#orderProduct.productBarcode.color.name"/></td>
							      <td><s:property value="#orderProduct.productBarcode.barcode"/></td>
							      <td><s:property value="#orderProduct.productBarcode.product.unit"/></td>
							      <td><s:property value="#orderProduct.productBarcode.product.salesPrice"/></td>
							      <td><s:property value="#orderProduct.quantity"/></td>
							      <td><s:property value="#orderProduct.totalSalesPrice"/></td>
							      <td><s:property value="#orderProduct.comment"/></td>
							     </tr>
							   </s:iterator>  
						  </tbody>
						  <tr class="PBAInnerTableTitale">
						    <th height="25" colspan="9" align='left'>合计</th>
						    <th align='center'><s:property value="uiBean.flowOrder.totalQuantity"/></th>
						    <th align='center'><s:property value="uiBean.flowOrder.totalSalesPrice"/></th>
						    
						    <th></th>
						  </tr>
					     </table>
			      </td>
			    </tr>
			    <tr class="InnerTableContent">
			      <td height="30" colspan="7">
			            <!-- table to show the footer of the chain inventory flow -->
						<table width="100%" align="left" border="0">
						   <tr class="InnerTableContent">
					         <td height="40">备注</td>
					         <td colspan="2"><s:property value="uiBean.flowOrder.comment"/></td>
				           </tr>
						  <tr class="InnerTableContent">
						    <td width="5%" height="25" align='left'>&nbsp;</td>
						    <td width="3%" align='left'>&nbsp;</td>
						    <td width="90%" align='left'>
						      <s:if test="#session.LOGIN_CHAIN_USER.containFunction('inventoryFlowJSPAction!cancelOrder') && formBean.canCancel && (uiBean.flowOrder.chainStore.chain_id == #session.LOGIN_CHAIN_USER.myChainStore.chain_id || #session.LOGIN_CHAIN_USER.roleType.chainRoleTypeId == 1 || #session.LOGIN_CHAIN_USER.roleType.chainRoleTypeId == 2)">
						            <input type="button" value="红冲单据" onclick="cancelOrder();"/>	
						      </s:if>
						      <s:if test="formBean.flowOrder.id > 0">
						            <input type="button" value="下载单据" onclick="downloadOrderTwo();"/>	
						      </s:if>
						      <input type="button" value="打印单据到POS机" onclick="printOrder();"/>	
						    </td>
					      </tr>
					    </table>
				  </td>
			    </tr>
			    <tr class="InnerTableContent">
			      <td height="4" colspan="7"><hr width="100%" color="#FFCC00"/></td>
			    </tr>
			  </table>
	   </td></tr>
	 </table>
	 </s:form>
</body>
<script type="text/javascript" src="<%=request.getContextPath()%>/conf_files/js/ChainInvenTransfer.js"></script>
</html>